package com.timothy.junk.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.ToggleButton;
import com.sina.util.StringUtil;
import com.timothy.junk.R;
import com.timothy.junk.tfund.bean.TFundNotify;
import java.util.List;

public class TFundNotifyAdaptor extends BaseAdapter {
	
	public static final double MAX_CEIL = 0.2;
	public static final double MIN_CEIL = 0.01;
	public static final double GRAD = 0.01;
	
	private Context mContext;
	private SharedPreferences sp;
	private List<TFundNotify> listItems;
	private LayoutInflater mInflater;

	static class ViewHolder{
		public TextView sTFundIndex;
        public TextView sTFundName;
		public EditText sTFundCeilYReturn;
		public ImageButton sAddCeilReturn;
		public ToggleButton sIsTFundNotify;
	}

	public TFundNotifyAdaptor(Context context, List<TFundNotify> data,SharedPreferences sp) {
		this.mContext = context;
		mInflater = LayoutInflater.from(context); 
		this.listItems = data;
	}

	
	@Override
	public int getCount() {
		return listItems.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;  
        if (convertView == null){  
            holder = new ViewHolder();  
            convertView = mInflater.inflate(R.layout.tfund_notify_list_item, null);
            
            holder.sTFundIndex = (TextView) convertView.findViewById(R.id.sTFundIndex);
			holder.sTFundName = (TextView) convertView.findViewById(R.id.sTFundName);
			holder.sTFundCeilYReturn = (EditText) convertView.findViewById(R.id.sTFundYReturn);
			holder.sAddCeilReturn = (ImageButton) convertView.findViewById(R.id.sAddCeilReturn);
			holder.sIsTFundNotify = (ToggleButton) convertView.findViewById(R.id.sIsTFundNotify);
			
            convertView.setTag(holder);  
        }else {  
            holder = (ViewHolder) convertView.getTag();  
        }

        final int p = position;
        
        TFundNotify bean = listItems.get(position);
		holder.sTFundIndex.setText(String.valueOf(position+1));
		holder.sTFundName.setText(bean.getTfundName());
		holder.sTFundCeilYReturn.setText(StringUtil.formatData2Deci(bean.getCeilYReturn()*100)+"%");
		
		holder.sAddCeilReturn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				changeCeil(p);
			}
		});
		
		if(bean.isNotify()){
			holder.sIsTFundNotify.setChecked(true);
		}else{
			holder.sIsTFundNotify.setChecked(false);
		}

        return convertView; 
        
	}
	
	public void changeCeil(int pos){
		TFundNotify tfNotify = listItems.get(pos);
		double oldCeil = tfNotify.getCeilYReturn();
		double newCeil = (oldCeil<MAX_CEIL)?oldCeil+GRAD:MIN_CEIL;
		tfNotify.setCeilYReturn(newCeil);
		listItems.set(pos, tfNotify);
		notifyDataSetChanged();
	}
	

}
